package com.fenghongzhang.day003_mvp.mvp.presenter;

import android.util.Log;

import com.fenghongzhang.day003_mvp.FoodModule;
import com.fenghongzhang.day003_mvp.entity.Food;
import com.fenghongzhang.day003_mvp.mvp.contract.FoodContract;
import com.fenghongzhang.lib.mvp.presenter.BasePresenter;
import com.google.gson.Gson;

import java.io.IOException;

import javax.inject.Inject;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 控制view 和model的交互
 */
public class FoodPresenter extends BasePresenter<FoodContract.IFoodModel,FoodContract.IFoodView> {

    private static final String TAG = "FoodPresenter";

    @Inject
    public FoodPresenter(FoodContract.IFoodModel mModel, FoodContract.IFoodView mView) {
        super(mModel, mView);
    }

    public void getData(){
        Log.i(TAG, "getData: ");
        mModel.getFoodData(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i(TAG, "onResponse: ");
                String string = response.body().string();
                Food food = new Gson().fromJson(string, Food.class);
                mView.showFood(food.getData());
            }
        });
    }
}
